<template>
  <div class="app-main-container">
    <QueryForm v-model="filters" :query-form-config="queryFormConfig" @handleQuery="getList(1)" @clear="clearFilters" />
    <div v-if="meta.indexOf('SortsListAdd') > -1" class="doumee-table-buttons">
      <el-button type="primary" @click="$router.push({ name: 'SortsListEdit' })">{{ translate("add") }}</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" element-loading-text="Loading" border fit :header-row-class-name="'table-header'" class="doumee-element-table">
      <el-table-column align="center" :label="translate('title')" prop="title" min-width="200" show-overflow-tooltip />
      <el-table-column align="center" :label="translate('listPic')" prop="img2" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-image fit="contain" :src="scope.row.img2" :preview-src-list="[scope.row.img2]" />
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('content')" prop="info" min-width="120" show-overflow-tooltip>
        <template slot-scope="scope">
          <span style="color: #435ebe; cursor: pointer" @click="showBtn(scope.row)">{{ translate('clickCheck') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('applyVenue')" prop="venueNames" min-width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-tooltip effect="dark" :content="scope.row.venueNames.join(',')" placement="top-start">
            <span class="han1">{{ scope.row.venueNames.join(",") }}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('status')" min-width="120" fixed="right" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#435EBE" inactive-color="#999999" :active-value="'1'" :inactive-value="'0'" :disabled="meta.indexOf('SortsListEdit') == -1" @change="toConfigUpdate(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column align="center" :label="translate('sort')" prop="sort" min-width="100" show-overflow-tooltip />
      <el-table-column align="center" :label="translate('createTime')" prop="createDate" min-width="160" />
      <el-table-column :label="translate('handle')" fixed="right" align="center" min-width="150">
        <template slot-scope="scope">
          <div class="flex-cc">
            <el-button v-if="meta.indexOf('SortsListEdit') > -1" v-preventReClick type="text" @click="
                $router.push({
                  name: 'SortsListEdit',
                  query: { id: scope.row.id },
                })
              ">{{ translate("edit") }}</el-button>
            <el-button v-if="meta.indexOf('KnowledgeDel') > -1" v-preventReClick type="text" style="color: red" @click="toDelete(scope.row.id)">{{ translate("del") }}</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!-- <div class="flex-cc doumee-pagination">
      <el-pagination
        background
        hide-on-single-page
        layout="prev, pager, next"
        :total="totalCount"
        @current-change="getList"
      />
    </div> -->
    <Pagination :total="totalCount" :page="pagination.page" @current-change="getList" @SizeChange="handleSizeChange" />
    <el-dialog :close-on-click-modal="modalDialog" :lose-on-press-escape="modalDialog" :title="translate('content')" :visible.sync="show">
      <div class="fwxInfo" v-html="info" />
    </el-dialog>
  </div>
</template>

<script>
import { deleteKnowledge, listKnowledge, editKnowledge, venueList } from '@/api'
import { parseTime } from '@/utils/index'
import Pagination from '@/components/paginations'
import { queryFormConfig } from './config'
export default {
  name: 'SortsList',
  components: {
    Pagination
  },
  data () {
    return {
      modalDialog: this.$store.state.app.dialog,
      queryFormConfig,
      pagination: {
        page: 1,
        rows: 10
      },
      list: [],
      totalCount: 0,
      listLoading: true,
      filters: {},
      show: false,
      info: '',
      meta: []
    }
  },
  created () {
    this.meta = this.$route.meta.buttons || []
    this.getVenueList()
    this.getList(1)
  },
  mounted () {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
  },
  activated () {
    this.$nextTick(() => {
      this.$refs.table.doLayout()
    })
    if (this.$route.query.renovate && this.$route.query.renovate === '1') {
      this.getList()
    }
  },
  methods: {
    showBtn (e) {
      this.show = true
      this.info = e.info
    },
    handleSizeChange (value) {
      this.pagination.rows = value
      this.getList(1)
    },
    toDelete (id) {
      const that = this
      this.$confirm(this.translate('sportKnowDelPlace'), this.translate('ts'), {
        confirmButtonText: this.translate('qd'),
        cancelButtonText: this.translate('cancel'),
        type: 'warning'
      })
        .then(() => {
          deleteKnowledge({ param: { id } }).then((res) => {
            if (res.errorCode !== '000000') return
            that.$message.success(this.translate('delScs'))
            let totalPage = Math.ceil((this.totalCount - 1) / this.pagination.rows)
            let currentPage = this.pagination.page > totalPage ? totalPage : this.pagination.page
            this.pagination.page = currentPage < 1 ? 1 : currentPage
            that.getList(this.pagination.page)
          })
        })
    },
    // 获取场馆列表
    getVenueList () {
      venueList({
        param: {}
      })
        .then((res) => {
          if (res.errorCode !== '000000') return
          const arr = res.recordList
          arr.forEach(e => {
            e.label = e.name
            e.value = e.id
          })
          this.queryFormConfig.formItems[1].options = arr
        })
    },
    toConfigUpdate (v) {
      const obj = { ...v }
      const arr = this.list
      const that = this
      this.listLoading = true
      editKnowledge({
        param: {
          id: obj.id,
          status: obj.status
        }
      })
        .then(res => {
          if (res.errorCode === '000000') {
            if (obj.status === '0') {
              that.$message.success(this.translate('jyScs'))
            } else {
              that.$message.success(this.translate('qyScs'))
            }
            that.getList(this.pagination.page)
          } else {
            arr.forEach(e => {
              if (e.id === obj.id) {
                e.status = obj.status === '1' ? '0' : '1'
              }
            })
            that.list = arr
            that.listLoading = false
          }
        }).catch(() => {
          arr.forEach(e => {
            if (e.id === obj.id) {
              e.status = obj.status === '1' ? '0' : '1'
            }
          })
          that.list = arr
          that.listLoading = false
        })
    },
    getList (page) {
      const { pagination, filters: param } = this
      if (page) {
        pagination.page = page
        this.pagination.page = page
      }
      if (pagination.page === 1) {
        pagination.firstQueryTime = parseTime(new Date())
      }
      param.type = '1'
      this.listLoading = true
      listKnowledge({
        pagination,
        param
      }, sessionStorage.getItem('venueId')).then(res => {
        this.listLoading = false
        if (res.errorCode === '000000') {
          this.totalCount = res.totalCount
          this.list = res.recordList
        }
      }).catch(err => {
        this.listLoading = false
        console.log('-----', err)
      })
    },
    // 清空筛选项
    clearFilters () {
      this.filters = {}
      this.getList(1)
    }
  }
}
</script>

<style lang="scss" scoped>
.han1 {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>
